#-------------------------------------------------------------------------------
# Name:        processor
# Purpose:
#
# Author:      TheAer
#
# Created:     01.10.2012
# Copyright:   (c) TheAer 2012
# Licence:     CC by-nc-sa
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import di     # English dictionary

NOT_IMPLEMENTED = 0xFF
NOT_READY = 0xFE
INT = 0x00
STR = 0x01
ARR = 0x02
LIB = 0x03
TYPE = 0x04
LIST = 0x05
SENTENCE = 0x10
QUESTION = 0x11
ORDER = 0x12
AFFIRMATION = 0x13
SUBJ = 'subj'
OBJI = 'indo'
OBJD = 'diro'
ADVRB = 'advrb'
VRB = 'verb'
ATTR = 'attr'
PREP = 'prep'
ATTRS = 'attrs'
AFTER = 'aftr'
BEFOR = 'befr'
PRED = 'pred'
BLDNR = (0,1)

def isin(fr, to):
    for el in fr:
        if el in to:
            return True
    return False


def process(text, schemes, order):
    text = text.split()
    d = di.Dict('en')
    sentParts = {}
    bufr = []
#    prep = ''
#    last = ''

    i = 0
    counter = {}
    if 'question-word' in d[text[0]]:
        SentType = 'question'
    for el in order:
        counter[el] = 0
    while i < len(text):
        word = text[i]
        partsOfSpeech = d[word]
        print schemes.keys()
        for el in order:
            BEFORE, SEEK, AFTER, MAXNO = schemes[el];
            print el
            matched = 0
            print partsOfSpeech, SEEK
            if MAXNO != 0 and counter[el] >= MAXNO:     # If maximal nr of matching words is reached, go to next word
                continue

            if isin(partsOfSpeech, SEEK):               # If word fits to scheme
                if BEFORE and not AFTER:
                    print d[text[i-1]], BEFORE          # Print word before and type of speech it should be of to match
                    if isin(d[text[i-1]], BEFORE):      # If they match
                        print '  OCCURENCE "BEFORE" MATCHED'
                        matched = 1
                elif AFTER and not BEFORE and i < len(text) - 1:
                    print d[text[i+1]], AFTER           # Print word after and type of speech it should be of to match
                    if isin(d[text[i+1]], AFTER):       # If they match
                        print '  OCCURENCE "AFTER" MATCHED'
                        matched = 1
                elif AFTER != None and BEFORE != None:
                    print d[text[i+1]], AFTER           # Print word after and type of speech it should be of to match
                    print d[text[i-1]], BEFORE          # Print word before and type of speech it should be of to match
                    if isin(d[text[i+1]], AFTER):       # If they match
                        print '  OCCURENCE "AFTER" MATCHED'
                        matched = 1
                else:
                    print '  1ST LVL OCCURENCE MATCHED'
                    matched = 1
            if matched:
                counter[el] += 1
                if counter[el] == MAXNO:
                    print 'MAXIMAL OCCURENCES REACHED FOR', el
                print 'MATCH', el, word, counter[el], '/', schemes[el][1]
                sentParts[el] = word
                break
        if not matched:
            print 'NO MATCH FOR', word

        i += 1
    return sentParts

"""    schemes = {OBJI: ([PREP, ['noun', 'pronoun']], 0, VRB),
               SUBJ: ([['noun', 'pronoun']], 1, None),
               OBJD: ([['noun', 'pronoun']], 1, VRB),
               VRB: ([['verb']], 1, None),
               PREP: ([['prep']], 0, AFTER),
               ATTR: ([['ukwn']], 0, BEFOR),
               ADVRB: ([['ukwn']], 0, AFTER)
              }""";

orderDeclaration = [SUBJ, VRB, OBJI, OBJD]
schemesDeclaration = {SUBJ: (None, ['noun', 'pronoun'], None, 1),
                      VRB: (None, ['verb'], None, 1),
                      OBJI: (['prep'], ['noun', 'obj-pronoun'], None, 1),
                      OBJD: (['verb'], ['noun', 'pronoun'], None, 1),
                     }

orderQuestion = [SUBJ, VRB, OBJI, OBJD]
schemesQuestion = {SUBJ: (None, ['noun', 'pronoun'], None, 1),
                   VRB: (None, ['verb'], None, 1),
                   OBJI: (None, ['noun', 'obj-pronoun'], ['prep'], 1),
                   OBJD: (['noun'], ['noun', 'pronoun'], None, 1),
                  }

#sent = 'big cheetahs run quick to great forest from a house'
sent = 'man gives bone to dog'
sent = process(sent, schemesDeclaration, orderDeclaration)
print sent

sent = 'who gives man bone to'
sent = process(sent, schemesQuestion, orderQuestion)
print sent
#sent = process('to home send i')
